﻿<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeFile="PhieuChuyenKho.aspx.cs"
    Inherits="WebTeamplate.Admin_PhieuChuyenKho" %>

<%@ Import Namespace="WebTeamplate" %>
<%@ Register Assembly="Control" Namespace="Kng.Controls" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <script language="javascript" type="text/javascript" src="../Js/actb.js"></script>

        <script language="javascript" type="text/javascript" src="../Js/commonAutocomplete.js"></script>

        <script src="../Js/Popup.js"></script>

        <script src="../Js/BasicFunction.js"></script>

        <script src="../Js/ClientScript.js"></script>

        <script src="../Js/formatNumber.js"></script>

        <link href="../Js/calendar/calendar-win2k-cold-1.css" rel="stylesheet" type="text/css" />

        <script src="../Js/DateFunction.js"></script>

        <script type="text/javascript" src="../Js/calendar/calendar.js"></script>

        <script type="text/javascript" src="../Js/calendar/calendar-setup.js" language="javascript"></script>

        <script type="text/javascript" src="../Js/calendar/lang/calendar-en.js"></script>

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <table class="listForm" border="0" cellpadding="0" cellspacing="1" width="1200" align="center">
            <tr>
                <td colspan="8" class="detailedViewHeader">
                    <b>Quản lý phiếu chuyển hàng</b>
                    <asp:Button ID="btnDefault" runat="server" CssClass="buttonHidden" Text="" CausesValidation="false"
                        OnClientClick="return DefaultEnter();" />
                </td>
            </tr>
            <tr class="rowDetail" align="right">
                <td colspan="8" class="dvtCellInfo">
                    (<span class="commentRequireInput">*</span>): <span class="commentRequireInputText">
                        <%=Resources.Resource.LABEL_COMMENT_REQUIRE %>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="dvtCellLabel">
                    <asp:Label ID="lblKhoFrom" runat="server" Text="Chuyển từ khu"></asp:Label>(<span
                        class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" width="130px" align="left">
                    <asp:DropDownList ID="drlKhoFrom" CssClass="listForm ctrListForm" runat="server"
                        AutoPostBack="true" Width="120px" OnSelectedIndexChanged="drlKhoFrom_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
                <td class="dvtCellLabel">
                    <asp:Label ID="lblKhoTo" runat="server" Text="Đến khu"></asp:Label>(<span class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" align="left">
                    <asp:DropDownList ID="drlKhoTo" CssClass="listForm ctrListForm" runat="server" Width="120px">
                    </asp:DropDownList>
                </td>
                <td align="right" class="dvtCellLabel">
                    <%-- <asp:Label ID="lblChungXe" runat="server" Text="Chủng xe"></asp:Label>(<span class="commentRequireInput">*</span>):--%>
                    &nbsp;
                </td>
                <td class="dvtCellInfo" width="120px" align="left" colspan="3">
                    <asp:DropDownList ID="drlChungXe" Visible="false" CssClass="listForm ctrListForm"
                        runat="server" Width="120px" AutoPostBack="True" OnSelectedIndexChanged="drlChungXe_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr class="rowDetail">
                <td class="dvtCellLabel" width="120px">
                    Ngày lập phiếu(<span class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" width="130px" align="left">
                    <asp:TextBox ID="txtNgayNhap" runat="server" Width="90px" MaxLength="10" onblur="this.className='detailedViewTextBoxList1'"
                        onfocus="this.className='detailedViewTextBoxListOn1'" CssClass="detailedViewTextBoxList1"></asp:TextBox>
                    <img id="imgNgayNhap" alt="" title="Ngày nhập" style="cursor: hand; text-align: left;
                        vertical-align: middle; margin-bottom: 4px;" src="../images/imgCalendar.gif" />
                </td>
                <td width="130px" align="right" class="dvtCellLabel">
                    <asp:Label ID="lblNguoiNhap" runat="server" Text="Người lập phiếu"></asp:Label>(<span
                        class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" width="120px" align="left">
                    <asp:TextBox ID="txtNguoiNhap" runat="server" Width="180px" MaxLength="100" Enabled="false"
                        Text="Nguyen Van Ty" CssClass="detailedViewTextBoxList_Disable1 ctrListForm"></asp:TextBox>
                </td>
                <td width="110px" align="right" class="dvtCellLabel">
                </td>
                <td class="dvtCellInfo">
                </td>
            </tr>
            <tr>
                <td colspan="8" class="dvtCellInfo">
                    <hr size="1" noshade="noshade">
                </td>
            </tr>
            <tr>
                <td colspan="8" class="dvtCellInfo">
                    <%=form %>
                </td>
            </tr>
            <tr class="rowDetail" align="right" id="trAddRowControl" runat="server" visible="false">
                <td colspan="8" class="dvtCellListInfo">
                    <table width="100%" cellspacing="0" cellpadding="0" border="0" class="listForm">
                        <tr>
                            <td align="right">
                                <input type="button" value="Thêm dòng nhập" onclick="AddRow();" id="btnAddRow">
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="height: 40px;" valign="bottom" colspan="8" align="center">
                    <asp:Button ID="btnSave" runat="server" CssClass="crmbutton 
listForm edit" Text="Save" OnClick="btnSave_Click" CausesValidation="false" OnClientClick="return ValidatePhieuChuyenHang()" />
                    <asp:Button ID="btnXemBanIn" runat="server" CssClass="crmbutton
listForm edit" Text="Xem chi tiết" CausesValidation="false" Width="90px" Visible="false" />
                    <asp:Button ID="btnReset" runat="server" CssClass="crmbutton 
listForm edit" Text="Save" OnClick="btnReset_Click" CausesValidation="false" />
                    <asp:Button ID="btnCacel" runat="server" CssClass="crmbutton 
listForm edit" Text="Cancel" OnClick="btnCacel_Click" CausesValidation="false" />
                </td>
            </tr>
        </table>
        <input type="hidden" id="hidMaMH" runat="server" value="" />

        <script language="javascript" type="text/javascript">
            var txtNgayNhap = document.getElementById("<%= txtNgayNhap.ClientID %>");
            SetupCalendar(txtNgayNhap.id, "imgNgayNhap");     
        </script>

        <asp:Literal ID="ltrScript" runat="server">
        </asp:Literal>

        <script type="text/javascript">
            var accessMaMH = '';
            function ValidatePhieuChuyenHang() {

                //check require kho
                var khoFrom = document.getElementById('<%=drlKhoFrom.ClientID %>').value;
                if (khoFrom == '') {
                    alert('Vui lòng chọn khu chuyển hàng');
                    document.getElementById('<%=drlKhoFrom.ClientID %>').focus();
                    return false;
                }
                var khoTo = document.getElementById('<%=drlKhoTo.ClientID %>').value;
                if (khoTo == '') {
                    alert('Vui lòng chọn khu chuyển hàng');
                    document.getElementById('<%=drlKhoTo.ClientID %>').focus();
                    return false;
                }
                var ngayNhap = document.getElementById('<%=txtNgayNhap.ClientID %>').value;
                if (!isDate(ngayNhap, "dd/MM/yyyy")) {
                    alert('Ngày nhập không hợ lệ');
                    document.getElementById('<%=txtNgayNhap.ClientID %>').focus();
                    return false;
                }
                //check valida date here
                //check Phieu Nhap Hang Data
                /////////////////
                var data = '';
                
                var totalRow = document.getElementById('<%=hidTotalRow.ClientID %>').value;
                totalRow = parseInt(totalRow, 0);
                var inValidData = 1;
                
                for (var i = 1; i <= totalRow; i++) {
                    var tr = document.getElementById('tr_' + i);
                    if (tr) {
                        if (tr.style.display == '') {
                            var matHang = document.getElementById('txtMaMH_' + i).value;
                            if (matHang != '') {
                                if (!ValidataRow(i)) {
                                    alert('Vui lòng kiểm tra lại dữ liệu nhập tại dòng ' + inValidData);
                                    return false;
                                }
                                else {

                                    //MaMH|DVT|SL|GiaNhap
                                    var txtMaMH = document.getElementById('txtMaMH_' + i).value;
                                    var soluong = document.getElementById('txtSoLuongChuyen_' + i).value;
                                    soluong = parseFloat(soluong);
                                    if (isNaN(soluong))
                                        soluong = 0;
                                    var dvt = document.getElementById('drlDonViTinh_' + i).value;
                                    data += txtMaMH + ";" + soluong + ";" + dvt + "*";

                                }

                            }
                            inValidData++;

                        }
                    }
                }
              
              
                if (data == '') {
                    document.getElementById('<%=btnSave.ClientID %>').focus();
                    alert('Vui lòng nhập thông tin từng mặt hàng cần chuyển');
                    return false;
                }

                var nguoiNhap = '<%=userNameLogin %>';
                var command = '<%=Command %>';
                var MaPhieuPhap = '<%=MaPhieuChuyen %>';
                // alert(data);
                //Get Data and Insert DB
                Admin_PhieuChuyenKho.InsertData(command, MaPhieuPhap, khoFrom, khoTo, '', nguoiNhap, ngayNhap, data, InsertPhieuNhapHangResult);
                return false;
            }
            function ValidataRow(index) {

                
                //check require SoLuongChuyen
                var soluong = document.getElementById('txtSoLuongChuyen_' + index).value;
                if (!IsValidInt(soluong)) {
                    document.getElementById('txtSoLuongChuyen_' + index).focus();
                    return false;
                }

                //IsValidInt
                var dvt = document.getElementById('drlDonViTinh_' + index).value;
                if (dvt == '') {
                    document.getElementById('drlDonViTinh_' + index).focus();
                    return false;
                }
                
                return true;

            }
            function InsertPhieuNhapHangResult(response) {

                if (response.value == '0') {
                    alert('Có lỗi xảy ra trong quá trình truy cập dữ liệu. Vui lòng thử lai.!');
                    return false;
                }
                else {

                    window.location.href = 'ViewPhieuChuyenHang.aspx?id=' + response.value;

                }

            }
            function CheckSoLuongChuyen(value, maMH) {
                return true;
            }
            function CheckSoLuongTonMatHang(maMH, dvt) {
                accessMaMH = maMH;
                var kho = document.getElementById('<%=drlKhoFrom.ClientID %>').value;
                Admin_PhieuChuyenKho.GetSoLuongTon(maMH, kho, dvt, GenerateSoLuongTon);

            }
            function GenerateSoLuongTon(response) {
                document.getElementById('txtSoLuongTon_' + accessMaMH).value = response.value;
            }
            function DeleteRow(maMH) {
                var row = document.getElementById('tr_' + maMH);
                if (row) {
                    row.style.display = 'none';
                }
            }
            function RowHover(obj) {
                if (obj)
                    obj.className = 'lvtColDataHover';
            }
            function RowOut(obj) {
                if (obj)
                    obj.className = 'lvtColData';
            }
            function TextBoxFocus(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxListOn1 txtNumber';
            }
            function TextBoxBlurGroupNumber(obj) {

                if (obj) {
                    obj.className = 'detailedViewTextBoxList1 txtNumber';
                    obj.value = formatNumberAutoSeparate(obj.value);
                }
            }
            function TextBoxBlur(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxList1 txtNumber';
            }
            function TextBoxLeftFocus(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxListOn1';
            }
            function TextBoxLeftBlur(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxList1';
            }
            var rowMT = 1;
            function GetMHByname(value, index) {
                var maMH = Right(value, 6);
                rowMT = index;
                var kho = document.getElementById('<%=drlKhoFrom.ClientID %>').value;
                
                Admin_PhieuChuyenKho.GeMatHangInfor(maMH, kho, index, GetMatHangInfor);
            }
            function GetMHByMaPT(value, row) {

                rowMT = row;
                var kho = document.getElementById('<%=drlKhoFrom.ClientID %>').value;
                Admin_PhieuChuyenKho.GeMatHangInforByMaPT(value, kho, row, GetMatHangInforByMaPT);
            }
            function GetMatHangInforByMaPT(response) {

                if (rowMT > 0) {
                    var arr = new Array();

                    arr = response.value.split('*+*');

                    if (arr.length > 1) {

                        SetControlvalue(document.getElementById('txtMaMH_' + rowMT), GetValueArray(arr, 0));
                        SetControlvalue(document.getElementById('txtMaPT_' + rowMT), GetValueArray(arr, 1));
                        SetControlvalue(document.getElementById('txtXe_' + rowMT), GetValueArray(arr, 2));
                        SetControlvalue(document.getElementById('txtNuocSX_' + rowMT), GetValueArray(arr, 3));
                        SetControlvalue(document.getElementById('txtHieu_' + rowMT), GetValueArray(arr, 4));
                        SetControlvalue(document.getElementById('txtTenMH_' + rowMT), GetValueArray(arr, 5));
                        var lstDVT = GetValueArray(arr, 5);
                        if (lstDVT != '') {
                            document.getElementById('tdDVT_' + rowMT).innerHTML = GetValueArray(arr, 6);
                            SetControlvalue(document.getElementById('drlDonViTinh_' + rowMT), GetValueArray(arr, 7));
                        }
                        SetControlvalue(document.getElementById('txtSoLuongTon_' + rowMT), GetValueArray(arr, 8));
                    }

                }
                rowMT = 0;
            }
            function GetMatHangInfor(response) {
                
                if (rowMT > 0) {
                    var arr = new Array();

                    arr = response.value.split('*+*');
                    
                    if (arr.length > 1) {
                        
                        var maPT = GetValueArray(arr, 1);
                        var maMH = GetValueArray(arr, 0);
                        SetControlvalue(document.getElementById('txtMaMH_' + rowMT), maMH);
                        SetControlvalue(document.getElementById('txtMaPT_' + rowMT), GetValueArray(arr, 1));
                        SetControlvalue(document.getElementById('txtXe_' + rowMT), GetValueArray(arr, 2));
                        SetControlvalue(document.getElementById('txtNuocSX_' + rowMT), GetValueArray(arr, 3));
                        SetControlvalue(document.getElementById('txtHieu_' + rowMT), GetValueArray(arr, 4));
                        var lstDVT = GetValueArray(arr, 5);
                        if (lstDVT != '') {
                            document.getElementById('tdDVT_' + rowMT).innerHTML = lstDVT;
                            SetControlvalue(document.getElementById('drlDonViTinh_' + rowMT), GetValueArray(arr, 6));
                        }
                        SetControlvalue(document.getElementById('txtSoLuongTon_' + rowMT), GetValueArray(arr, 7));

                        if (maMH != '') {
                            if (maPT == '') {
                                document.getElementById('txtMaPT_' + rowMT).disabled = true;
                                document.getElementById('txtMaPT_' + rowMT).className = 'detailedViewTextBoxList_Disable1 ctrListForm';
                            }
                            else {
                                document.getElementById('txtMaPT_' + rowMT).className = 'detailedViewTextBoxList_Disable ctrListForm';
                                document.getElementById('txtMaPT_' + rowMT).disabled = false;
                            }
                        }
                        else {
                            document.getElementById('txtMaPT_' + rowMT).className = 'detailedViewTextBoxList_Disable ctrListForm';
                            document.getElementById('txtMaPT_' + rowMT).disabled = false;
                        }

                    }
                    else SetControlvalue(document.getElementById('txtMaMH_' + rowMT), '');
                }
                rowMT = 0;
            }
            function SetControlvalue(ctr, value) {
                try {
                    if (ctr)
                        ctr.value = value;
                }
                catch (e) {
                }
            }
            function GetValueArray(arr, index) {
                try {
                    return arr[index].toString()
                }
                catch (e) {
                    return '';
                }
            }
            function CheckSoLuongTonMatHang(row, dvt) {
                rowMT = row;
                var kho = document.getElementById('<%=drlKhoFrom.ClientID %>').value;
                var maMH = document.getElementById('txtMaMH_' + row).value;
                Admin_PhieuChuyenKho.GetSoLuongTon(maMH, kho, dvt, GenerateSoLuongTon);

            }
            function GenerateSoLuongTon(response) {
                SetControlvalue(document.getElementById('txtSoLuongTon_' + rowMT), response.value);
            }
            function AddRow() {

                var maxRow = document.getElementById('<%=hidMaxRowShow.ClientID %>').value;
                maxRow = parseInt(maxRow, 0);
                maxRow += 1;

                var hidCurrentRowShow = document.getElementById('<%=hidCurrentRowShow.ClientID %>').value;
                hidCurrentRowShow = parseInt(hidCurrentRowShow, 0);
                hidCurrentRowShow = hidCurrentRowShow + 1;
                if (hidCurrentRowShow > maxRow)
                    return;

                document.getElementById('tr_' + maxRow).style.display = ''
                document.getElementById('<%=hidMaxRowShow.ClientID %>').value = maxRow;
                document.getElementById('<%=hidCurrentRowShow.ClientID %>').value = hidCurrentRowShow;
                document.getElementById('txtTenMH_' + maxRow).focus();

            }
            function DeleteRow(index) {
                var yes = confirm('Bạn muốn xóa dòng này?');
                //var yes = true;
                if (yes) {
                    if (document.getElementById('tr_' + index)) {

                        document.getElementById('tr_' + index).style.display = 'none';
                        var hidCurrentRowShow = document.getElementById('<%=hidCurrentRowShow.ClientID %>').value;
                        hidCurrentRowShow = parseInt(hidCurrentRowShow, 0);
                        hidCurrentRowShow = hidCurrentRowShow - 1;
                        document.getElementById('<%=hidCurrentRowShow.ClientID %>').value = hidCurrentRowShow;


                    }
                }
            }
            window.scrollTo = function() {

            }
            function DefaultEnter() {
                return false;
            }
        </script>

    </div>
    <input type="hidden" id="hidTotalRow" value="" runat="server" />
    <input type="hidden" id="hidMaxRowShow" value="" runat="server" />
    <input type="hidden" id="hidCurrentRowShow" value="" runat="server" />
    <input type="hidden" id="hidOptionLoaiMH" value="" runat="server" />
    <input type="hidden" id="hidOptionDVT" value="" runat="server" />
    <input type="hidden" id="hidRowValid" value="" runat="server" />
    </form>
</body>
</html>
